package com.suning.sawp.web.controller.model;

import java.security.Principal;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.model.ModelBindService;
import com.suning.sawp.intf.model.vo.BindResultInfo;
import com.suning.sawp.intf.model.vo.ModelBodyInfo;
import com.suning.sawp.intf.store.StoreInfoService;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 
 * 样机分析接口<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/model/bind")
public class ModelBindController extends DjBaseController {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ModelBindController.class);

    /**
     * 店员服务
     */
    @Autowired
    StoreManService storeManService;

    /**
     * 样机分析服务
     */
    @Autowired
    ModelBindService modelBindService;

    /**
     * 门店服务
     */
    @Autowired
    StoreInfoService storeInfoService;

    /**
     * 
     *
     * @param principal
     * @param commcode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryModelInfoByCommcode.do")
    public MyGsonView queryModelInfoByCommcode(Principal principal, String commCode) {
        LOGGER.info("queryModelInfoByCommcode enter, principal={},commcode={}", principal.getName(), commCode);
        ReturnMsg<ModelBodyInfo> returnMsg = new ReturnMsg<ModelBodyInfo>();
        // 工号
        String staffId = principal.getName();

        if (StringUtils.isBlank(commCode)) {
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0001);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
            return setGsonView(returnMsg);
        }
        // 返回对象
        try {
            // 查询店员信息
            StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
            ModelBodyInfo result = modelBindService.queryModelInfoByCommcode(commCode, storeMan.getStoreCode());
            returnMsg.setData(result);
        } catch (Exception e) {
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
            LOGGER.error("queryModelInfoByCommcode error,staffId=" + staffId + ",commCode=" + commCode, e);
        }
        LOGGER.info("queryModelInfoByCommcode exit, principal={},commCode={}", principal.getName(), commCode);
        return setGsonView(returnMsg);
    }

    @RequestMapping(value = "/bindModelBody.do", method = RequestMethod.POST)
    public MyGsonView bindModelBody(Principal principal, @RequestParam(value = "serialNo") String serialNo,
            @RequestParam(value = "bodyCode") String bodyCode, @RequestParam(value = "commCode") String commCode) {
        LOGGER.info("bindModelBody enter, principal={}, serialNo={},bodyCode={}", principal.getName(), serialNo,
                bodyCode);
        ReturnMsg<BindResultInfo> returnMsg = new ReturnMsg<BindResultInfo>();
        // 工号
        String staffId = principal.getName();

        if (StringUtils.isBlank(commCode) || StringUtils.isBlank(serialNo)) {
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0001);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
            return setGsonView(returnMsg);
        }
        // 返回对象
        try {
            returnMsg = modelBindService.bindModelBody(serialNo, bodyCode, staffId, commCode);
        } catch (Exception e) {
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
            LOGGER.error("bindModelBody error,staffId=" + staffId + ",commCode=" + commCode, e);
        }
        LOGGER.info("bindModelBody end, principal={}, serialNo={},bodyCode={}", principal.getName(), serialNo, bodyCode);
        return setGsonView(returnMsg);
    }
}
